package queue_stack.leetcode.medium;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author bruin_du
 * @description 二叉树最底层最左边的值
 * @date 2022/8/13 23:42
 **/
public class OfferII045_FindBottomLeftValue {
    public int findBottomLeftValue(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int size = 1, left = 0;
        while (!queue.isEmpty()) {
            int num = 0;
            left = queue.peek().val;
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                if (node.left != null) {
                    queue.offer(node.left);
                    num++;
                }
                if (node.right != null){
                    queue.offer(node.right);
                    num++;
                }
            }
            size = num;
        }
        return left;
    }
}
